package com.lwl.Utils;

import org.junit.Assert;

public class NumberUtils {

    public static void swap(int[] array, int i, int j) {
        if (i == j) {
            return;
        }
        array[i] += array[j];
        array[j] = array[i] - array[j];
        array[i] = array[i] - array[j];
    }

    /**
     * 生成含有重复元素的数组
     */
    public static int[] createRandomArrayRepeat(int len, int maxValue) {
        int[] arr = new int[len];
        for (int i = 0; i < len; i++) {
            arr[i] = (int) (Math.random() * maxValue) + 1;
        }
        return arr;
    }

    /**
     * 生成含有重复元素的数组
     * <p>
     * maxValue 需要大于maxLen
     */
    public static int[] randomArray(int len, int maxValue) {
        Assert.assertTrue(maxValue >= len);
        int[] arr = new int[len];
        boolean[] has = new boolean[maxValue + 1];
        for (int i = 0; i < len; i++) {
            do {
                arr[i] = (int) (Math.random() * maxValue) + 1;
            } while (has[arr[i]]);
            has[arr[i]] = true;
        }
        return arr;
    }

}
